//
//  TTMainView.swift
//  TTWeibo
//
//  Created by 陶宏路 on 16/7/19.
//  Copyright © 2016年 陶路路. All rights reserved.
//

import UIKit
import SnapKit


protocol VisitorViewDelegate: NSObjectProtocol{
    
    func LoginButton()
    
}

class TTVisitorView: UIView {

   weak var visitorDelegate:VisitorViewDelegate?
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
         backgroundColor = UIColor(red: 237/255, green: 237/255, blue: 237/255, alpha: 1)
        setUpUI()
        scrollAnima()
    } 
    
// 给外界提供接口 设置不同界面的view
    
    func setImageAndTitle(img:String?,title:String?){
    
        if let image = img ,let text = title{
            
            mainImage.image = UIImage(named: image)
            massageLable.text = text
            scrollImage.hidden = true
            
        }else{
            
//            scrollAnima()
        }
        
    }
    
// 设置中间imageview 的动画效果
    
    func scrollAnima(){
        
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = 2*M_PI
        anim.duration = 10
        anim.repeatCount = MAXFLOAT
        anim.removedOnCompletion = false
        scrollImage.layer.addAnimation(anim, forKey: nil)
    }

// 添加控件以及设置约束
    
    func setUpUI(){
        addSubview(scrollImage)
        addSubview(CoverImage)
        addSubview(mainImage)
        addSubview(massageLable)
        addSubview(loginButton)
        addSubview(regButton)
        
        scrollImage.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(self.snp_center)
        }
        
        CoverImage.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(self.snp_center)
        }
        
        mainImage.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(self.snp_center)
        }
        
        massageLable.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(CoverImage.snp_bottom).offset(16)
            make.size.equalTo(CGSizeMake(240, 36))
            make.centerX.equalTo(CoverImage)
        }
        
        loginButton.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(massageLable.snp_bottom).offset(16)
            make.size.equalTo(CGSizeMake(100, 36))
            make.leading.equalTo(massageLable)
        }
        regButton.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(massageLable.snp_bottom).offset(16)
            make.size.equalTo(CGSizeMake(100, 36))
            make.trailing.equalTo(massageLable)
        }

    }
    

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
//  懒加载控件
    
    lazy var scrollImage:UIImageView = {
       
        let scroll = UIImageView()
        scroll.image = UIImage(named: "visitordiscover_feed_image_smallicon")
        return scroll
    }()
    
    lazy var CoverImage:UIImageView = {
       let cover = UIImageView()
        cover.image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        return cover
    }()
    
    lazy var mainImage:UIImageView = {
       let main = UIImageView()
        main.image = UIImage(named: "visitordiscover_feed_image_house")
        return main
    }()
    
    lazy var massageLable:UILabel = {
        let lable = UILabel()
        lable.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        lable.font = UIFont.systemFontOfSize(15)
        lable.numberOfLines = 0
        lable.textAlignment = NSTextAlignment.Center
       return lable
    }()
    
    lazy var loginButton:UIButton = {
       let button = UIButton()
        button.setTitle("登录", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.addTarget(self, action: "didLoginButton", forControlEvents: UIControlEvents.TouchUpInside)
        return button
    }()
    
    lazy var regButton:UIButton = {
        let button = UIButton()
        button.setTitle("注册", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.addTarget(self, action: "didLoginButton", forControlEvents: UIControlEvents.TouchUpInside)
        return button
    }()

    func didLoginButton(){
        
        if self.visitorDelegate!.respondsToSelector("LoginButton"){
        
        visitorDelegate?.LoginButton()
            
        }
    }
}
